import { Component, Input } from '@angular/core';
import { CascaderItem } from 'ng-devui';

@Component({
  selector: 'app-edit-customer-type',
  templateUrl: './edit-customer-type.component.html',
  styleUrls: ['./edit-customer-type.component.scss']
})
export class EditCustomerTypeComponent {
  @Input() data: any
  form: any = {
    customerClassId: null,
    customerClassName: "",
    note: "",
    parentCustomerClassId: null,
    subCustomerClasses:[]
  }
  options = []
  pathPids: any = []
  constructor() { }
  ngOnInit(): void {
    this.form.customerClassName = this.data.option.customerClassName
    this.form.note = this.data.option.note
    this.pathPids = this.data.pathPids
    this.form.customerClassId = this.data.option.customerClassId
    this.form.parentCustomerClassId = this.pathPids[this.pathPids.length - 1]
    this.options = this.convertToCascaderItem(this.data.options)

  }
  convertToCascaderItem(data: any[]): CascaderItem[] {
    return data.map(item => {
      let cascaderItem: CascaderItem = {
        label: item.customerClassName,
        value: item.customerClassId,
        disabled: item.customerClassId === this.form.customerClassId
      };
      if (item.subCustomerClasses) {
        cascaderItem.children = this.convertToCascaderItem(item.subCustomerClasses);
      }
      return cascaderItem;
    });
  }//转换数据格式为华为库练级菜单的数据格式
  ngModelChange() {
    this.form.parentCustomerClassId = this.pathPids[this.pathPids.length - 1]
  }
}
